package 寒假刷题;

public class Z字形变换 {
    public String convert(String s, int numRows) {
        int n = s.length();
        if (numRows==1 || n<=numRows)
            return s;
        StringBuilder[] builders = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            builders[i] = new StringBuilder();
        }
        int idx = 0,flag = -1;
        for (int i = 0; i < n; i++) {
            builders[idx].append(s.charAt(i));
            if (idx==0 || idx==numRows-1)
                flag = -flag;
            idx+=flag;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            builder.append(builders[i]);
        }
        return builder.toString();
    }
}
